﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DalGen.Entity;
using System.Xml;

namespace DalGen.Util
{
    public class XmlHelper
    {
        public static List<EBuilderInfo> GetList()
        {

            List<EBuilderInfo> entityList = new List<EBuilderInfo>();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {

                EBuilderInfo entity = new EBuilderInfo()
                {
                    Guid = item["GUID"].InnerText,
                    Alias = item["Alias"].InnerText,
                    ConnectionString = item["ConnectionString"].InnerText,
                    DataBaseName = item["DataBaseName"].InnerText,
                    DataBaseType = item["DataBaseType"].InnerText,
                    DataBaseVer = item["DataBaseVer"].InnerText,
                    EntityNameSpace = item["EntityNameSpace"].InnerText,
                    EntitySavePath = item["EntitySavePath"].InnerText,
                    IRepositoryNameSpace = item["IRepositoryNameSpace"].InnerText,
                    IRepositorySavePath = item["IRepositorySavePath"].InnerText,
                    IServiceNameSpace = item["IServiceNameSpace"].InnerText,
                    IServiceSavePath = item["IServiceSavePath"].InnerText,
                    MapXmlSavePath = item["MapXmlSavePath"].InnerText,
                    Pwd = item["Pwd"].InnerText,
                    RepositoryNameSpace = item["RepositoryNameSpace"].InnerText,
                    RepositorySavePath = item["RepositorySavePath"].InnerText,
                    Server = item["Server"].InnerText,
                    ServiceNameSpace = item["ServiceNameSpace"].InnerText,
                    ServiceSavePath = item["ServiceSavePath"].InnerText,
                    UID = item["UID"].InnerText,
                };
                entityList.Add(entity);
            }


            return entityList;
        }

        public static List<EProjectInfo> GetProjectList()
        {
            List<EProjectInfo> entityList = new List<EProjectInfo>();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {

                EProjectInfo entity = new EProjectInfo();


                entity.Guid = item["Guid"].InnerText == null ? "" : item["Guid"].InnerText;
                entity.DataBaseName = item["DataBaseName"].InnerText == null ? "" : item["DataBaseName"].InnerText;
                entity.DataBaseType = item["DataBaseType"].InnerText == null ? "" : item["DataBaseType"].InnerText;
                entity.DataBaseVer = item["DataBaseVer"].InnerText == null ? "" : item["DataBaseVer"].InnerText;
                entity.Server = item["Server"].InnerText == null ? "" : item["Server"].InnerText;
                entity.UID = item["UID"].InnerText == null ? "" : item["UID"].InnerText;


                entityList.Add(entity);
            }
            return entityList;

        }


        public static EProjectInfo GetEntity(string guid)
        {
            EProjectInfo entity = new EProjectInfo();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {
                if (item["Guid"].InnerText == guid)
                {
                    entity = new EProjectInfo();
                    entity.Company = item["Company"].InnerText;
                    entity.CopyRight = item["CopyRight"].InnerText;
                    entity.DataBaseName = item["DataBaseName"].InnerText;
                    entity.DataBaseType = item["DataBaseType"].InnerText;
                    entity.DataBaseVer = item["DataBaseVer"].InnerText;
                    entity.Designer = item["Designer"].InnerText;
                    entity.Email = item["Email"].InnerText;
                    entity.EntityNameSpace = item["EntityNameSpace"].InnerText;
                    entity.EntitySavePath = item["EntitySavePath"].InnerText;
                    entity.Guid = item["Guid"].InnerText;
                    entity.IRepositoryNameSpace = item["IRepositoryNameSpace"].InnerText;
                    entity.IRepositorySavePath = item["IRepositorySavePath"].InnerText;
                    entity.IServiceNameSpace = item["IServiceNameSpace"].InnerText;
                    entity.IServiceSavePath = item["IServiceSavePath"].InnerText;
                    entity.MapXmlSavePath = item["MapXmlSavePath"].InnerText;
                    entity.ProjectNameSpace = item["ProjectNameSpace"].InnerText;
                    entity.ProjectSavePath = item["ProjectSavePath"].InnerText;
                    entity.Pwd = item["Pwd"].InnerText;
                    entity.RepositoryNameSpace = item["RepositoryNameSpace"].InnerText;
                    entity.RepositorySavePath = item["RepositorySavePath"].InnerText;
                    entity.Server = item["Server"].InnerText;
                    entity.ServiceModelNameSpace = item["ServiceModelNameSpace"].InnerText;
                    entity.ServiceModelSavePath = item["ServiceModelSavePath"].InnerText;
                    entity.ServiceNameSpace = item["ServiceNameSpace"].InnerText;
                    entity.ServiceSavePath = item["ServiceSavePath"].InnerText;
                    entity.TableName = item["TableName"].InnerText;
                    entity.Title = item["Title"].InnerText;
                    entity.UID = item["UID"].InnerText;
                    entity.Version = item["Version"].InnerText;


                    break;

                }


            }

            return entity;

        }

        //DataBaseName

        public static EProjectInfo GetByDataBaseName(string dataBaseName)
        {
            EProjectInfo entity = new EProjectInfo();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {
                if (item["DataBaseName"].InnerText == dataBaseName)
                {
                    entity = new EProjectInfo();
                    entity.Company = item["Company"].InnerText;
                    entity.CopyRight = item["CopyRight"].InnerText;
                    entity.DataBaseName = item["DataBaseName"].InnerText;
                    entity.DataBaseType = item["DataBaseType"].InnerText;
                    entity.DataBaseVer = item["DataBaseVer"].InnerText;
                    entity.Designer = item["Designer"].InnerText;
                    entity.Email = item["Email"].InnerText;
                    entity.EntityNameSpace = item["EntityNameSpace"].InnerText;
                    entity.EntitySavePath = item["EntitySavePath"].InnerText;
                    entity.Guid = item["Guid"].InnerText;
                    entity.IRepositoryNameSpace = item["IRepositoryNameSpace"].InnerText;
                    entity.IRepositorySavePath = item["IRepositorySavePath"].InnerText;
                    entity.IServiceNameSpace = item["IServiceNameSpace"].InnerText;
                    entity.IServiceSavePath = item["IServiceSavePath"].InnerText;
                    entity.MapXmlSavePath = item["MapXmlSavePath"].InnerText;
                    entity.ProjectNameSpace = item["ProjectNameSpace"].InnerText;
                    entity.ProjectSavePath = item["ProjectSavePath"].InnerText;
                    entity.Pwd = item["Pwd"].InnerText;
                    entity.RepositoryNameSpace = item["RepositoryNameSpace"].InnerText;
                    entity.RepositorySavePath = item["RepositorySavePath"].InnerText;
                    entity.Server = item["Server"].InnerText;
                    entity.ServiceModelNameSpace = item["ServiceModelNameSpace"].InnerText;
                    entity.ServiceModelSavePath = item["ServiceModelSavePath"].InnerText;
                    entity.ServiceNameSpace = item["ServiceNameSpace"].InnerText;
                    entity.ServiceSavePath = item["ServiceSavePath"].InnerText;
                    entity.TableName = item["TableName"].InnerText;
                    entity.Title = item["Title"].InnerText;
                    entity.UID = item["UID"].InnerText;
                    entity.Version = item["Version"].InnerText;


                    break;

                }


            }

            return entity;

        }

        public static IList<EProjectInfo> GetAll()
        {
            IList<EProjectInfo> list = new List<EProjectInfo>();


            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {
                EProjectInfo entity = new EProjectInfo();

                entity = new EProjectInfo();
                entity.Company = item["Company"].InnerText;
                entity.CopyRight = item["CopyRight"].InnerText;
                entity.DataBaseName = item["DataBaseName"].InnerText;
                entity.DataBaseType = item["DataBaseType"].InnerText;
                entity.DataBaseVer = item["DataBaseVer"].InnerText;
                entity.Designer = item["Designer"].InnerText;
                entity.Email = item["Email"].InnerText;
                entity.EntityNameSpace = item["EntityNameSpace"].InnerText;
                entity.EntitySavePath = item["EntitySavePath"].InnerText;
                entity.Guid = item["Guid"].InnerText;
                entity.IRepositoryNameSpace = item["IRepositoryNameSpace"].InnerText;
                entity.IRepositorySavePath = item["IRepositorySavePath"].InnerText;
                entity.IServiceNameSpace = item["IServiceNameSpace"].InnerText;
                entity.IServiceSavePath = item["IServiceSavePath"].InnerText;
                entity.MapXmlSavePath = item["MapXmlSavePath"].InnerText;
                entity.ProjectNameSpace = item["ProjectNameSpace"].InnerText;
                entity.ProjectSavePath = item["ProjectSavePath"].InnerText;
                entity.Pwd = item["Pwd"].InnerText;
                entity.RepositoryNameSpace = item["RepositoryNameSpace"].InnerText;
                entity.RepositorySavePath = item["RepositorySavePath"].InnerText;
                entity.Server = item["Server"].InnerText;
                entity.Port = item["Port"].InnerText;
                entity.ServiceModelNameSpace = item["ServiceModelNameSpace"].InnerText;
                entity.ServiceModelSavePath = item["ServiceModelSavePath"].InnerText;
                entity.ServiceNameSpace = item["ServiceNameSpace"].InnerText;
                entity.ServiceSavePath = item["ServiceSavePath"].InnerText;
                entity.TableName = item["TableName"].InnerText;
                entity.Title = item["Title"].InnerText;
                entity.UID = item["UID"].InnerText;
                entity.Version = item["Version"].InnerText;
                list.Add(entity);

            }

            return list;
        }

        public static EProjectInfo GeByGuid(string guid)
        {

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            var entity = new EProjectInfo();
            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {
                if (item["Guid"].InnerText == guid)
                {


                    entity.Company = item["Company"].InnerText;
                    entity.CopyRight = item["CopyRight"].InnerText;
                    entity.DataBaseName = item["DataBaseName"].InnerText;
                    entity.DataBaseType = item["DataBaseType"].InnerText;
                    entity.DataBaseVer = item["DataBaseVer"].InnerText;
                    entity.Designer = item["Designer"].InnerText;
                    entity.Email = item["Email"].InnerText;
                    entity.EntityNameSpace = item["EntityNameSpace"].InnerText;
                    entity.EntitySavePath = item["EntitySavePath"].InnerText;
                    entity.Guid = item["Guid"].InnerText;
                    entity.IRepositoryNameSpace = item["IRepositoryNameSpace"].InnerText;
                    entity.IRepositorySavePath = item["IRepositorySavePath"].InnerText;
                    entity.IServiceNameSpace = item["IServiceNameSpace"].InnerText;
                    entity.IServiceSavePath = item["IServiceSavePath"].InnerText;
                    entity.MapXmlSavePath = item["MapXmlSavePath"].InnerText;
                    entity.ProjectNameSpace = item["ProjectNameSpace"].InnerText;
                    entity.ProjectSavePath = item["ProjectSavePath"].InnerText;
                    entity.Pwd = item["Pwd"].InnerText;
                    entity.RepositoryNameSpace = item["RepositoryNameSpace"].InnerText;
                    entity.RepositorySavePath = item["RepositorySavePath"].InnerText;
                    entity.Server = item["Server"].InnerText;
                    entity.Port = item["Port"].InnerText;
                    entity.ServiceModelNameSpace = item["ServiceModelNameSpace"].InnerText;
                    entity.ServiceModelSavePath = item["ServiceModelSavePath"].InnerText;
                    entity.ServiceNameSpace = item["ServiceNameSpace"].InnerText;
                    entity.ServiceSavePath = item["ServiceSavePath"].InnerText;
                    entity.TableName = item["TableName"].InnerText;
                    entity.Title = item["Title"].InnerText;
                    entity.UID = item["UID"].InnerText;
                    entity.Version = item["Version"].InnerText;

                    break;
                }

            }

            return entity;
        }



        public static void AddDalInfo(EProjectInfo entity)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement root = xmlDoc.DocumentElement;

            XmlElement elemRoot = xmlDoc.CreateElement("DalProject");
            root.AppendChild(elemRoot);


            AddXmlElement(xmlDoc, elemRoot, "Title", entity.Title);
            AddXmlElement(xmlDoc, elemRoot, "Version", entity.Version);
            AddXmlElement(xmlDoc, elemRoot, "Company", entity.Company);
            AddXmlElement(xmlDoc, elemRoot, "Designer", entity.Designer);
            AddXmlElement(xmlDoc, elemRoot, "Email", entity.Email);
            AddXmlElement(xmlDoc, elemRoot, "CopyRight", entity.CopyRight);

            AddXmlElement(xmlDoc, elemRoot, "Guid", entity.Guid);
            AddXmlElement(xmlDoc, elemRoot, "Server", entity.Server);
            AddXmlElement(xmlDoc, elemRoot, "Port", entity.Port);
            AddXmlElement(xmlDoc, elemRoot, "DataBaseType", entity.DataBaseType);
            AddXmlElement(xmlDoc, elemRoot, "DataBaseVer", entity.DataBaseVer);
            AddXmlElement(xmlDoc, elemRoot, "UID", entity.UID);
            AddXmlElement(xmlDoc, elemRoot, "Pwd", entity.Pwd);
            AddXmlElement(xmlDoc, elemRoot, "DataBaseName", entity.DataBaseName);
            AddXmlElement(xmlDoc, elemRoot, "TableName", entity.TableName);

            AddXmlElement(xmlDoc, elemRoot, "ProjectNameSpace", entity.ProjectNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "ProjectSavePath", entity.ProjectSavePath);
            AddXmlElement(xmlDoc, elemRoot, "EntitySavePath", entity.EntitySavePath);
            AddXmlElement(xmlDoc, elemRoot, "MapXmlSavePath", entity.MapXmlSavePath);
            AddXmlElement(xmlDoc, elemRoot, "IRepositorySavePath", entity.IRepositorySavePath);
            AddXmlElement(xmlDoc, elemRoot, "RepositorySavePath", entity.RepositorySavePath);
            AddXmlElement(xmlDoc, elemRoot, "IServiceSavePath", entity.IServiceSavePath);
            AddXmlElement(xmlDoc, elemRoot, "ServiceSavePath", entity.ServiceSavePath);
            AddXmlElement(xmlDoc, elemRoot, "ServiceModelSavePath", entity.ServiceModelSavePath);
            AddXmlElement(xmlDoc, elemRoot, "ProjectNameSpace", entity.ProjectNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "EntityNameSpace", entity.EntityNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "IRepositoryNameSpace", entity.IRepositoryNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "RepositoryNameSpace", entity.RepositoryNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "IServiceNameSpace", entity.IServiceNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "ServiceNameSpace", entity.ServiceNameSpace);
            AddXmlElement(xmlDoc, elemRoot, "ServiceModelNameSpace", entity.ServiceModelNameSpace);
            xmlDoc.Save("dal.xml");

        }

        public static void Remove(string guid)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("dal.xml");

            XmlElement element = xmlDoc.DocumentElement;

            foreach (XmlElement item in element.SelectNodes("DalProject"))
            {
                if (item["Guid"].InnerText == guid)
                {

                    item.RemoveAll();
                    item.ParentNode.RemoveChild(item);
                    break;
                }

            }
            xmlDoc.Save("dal.xml");


        }

        public static void Update(EProjectInfo entity)
        {

            Remove(entity.Guid);
            AddDalInfo(entity);
        }

        private static void AddXmlElement(XmlDocument xmldoc, XmlElement elemRoot, string elementName, string value)
        {
            XmlElement elem = xmldoc.CreateElement(elementName);
            elem.InnerText = value;
            elemRoot.AppendChild(elem);

        }

    }
}
